﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Guitar.Vues;
using Modele;

namespace Guitar.Vues
{
    /// <summary>
    /// Logique d'interaction pour VueNote.xaml
    /// </summary>
    public partial class VueNote : Canvas
    {

        public Note note;
        private bool _IsSelected = false;

        public bool IsSelected
        {

            get { return _IsSelected; }
            set
            {
                _IsSelected = value; if (value)
                {

                    FondNote.Fill = (Brush)FindResource("selected");

                    FondNote.Visibility = Visibility.Visible;
                }
                else { FondNote.Visibility = Visibility.Hidden; }
            }


        }



        /* Permet d'instancier une note sur une portée */
        public VueNote()
        {
            InitializeComponent();
            note.OnRythmChanged += RythmChanged;
        }

        public VueNote(Note n)
        {
            InitializeComponent();
            note = n;
            note.OnRythmChanged += RythmChanged;
            Update();
        }

        private void Rectangle_MouseEnter(object sender, MouseEventArgs e)
        {
            FondNote.Visibility = Visibility.Visible;
            if (!IsSelected)
            {
                FondNote.Fill = (Brush)FindResource("over");

            }

        }

        private void Rectangle_MouseLeave(object sender, MouseEventArgs e)
        {
            if (!IsSelected)
            {
                FondNote.Visibility = Visibility.Hidden;
            }

        }

        private void UserControl_MouseDown(object sender, MouseButtonEventArgs e)
        {

            if (IsSelected)
            {
                IsSelected = false;
            }
            else
            {
                IsSelected = true;
            }

            App.lecteur.Lire(note);

        }

        /* Met à jour l'apparence de la note en fonction de ses caractéristiques */
        public void Update()
        {
            if (note.pointure == Note.Pointure.Pointe)
            {
                point.Visibility = Visibility.Visible;
            }
            else
            {
                point.Visibility = Visibility.Hidden;
            }

            croche1.Visibility = Visibility.Hidden;
            croche2.Visibility = Visibility.Hidden;
            croche3.Visibility = Visibility.Hidden;
            croche4.Visibility = Visibility.Hidden;


            switch (note.rythme)
            {
                case Note.TypeRytmique.Ronde:
                    Ellipsenote.Fill = null;
                    Ellipsenote.StrokeThickness = 1;
                    Ellipsenote.Stroke = Brushes.Black;

                    tigeBas.Visibility = Visibility.Hidden;
                    Tige.Visibility = Visibility.Hidden; break;

                case Note.TypeRytmique.Blanche:
                    Ellipsenote.Fill = null;
                    Ellipsenote.StrokeThickness = 1;
                    Ellipsenote.Stroke = Brushes.Black;
                    ; break;

                case Note.TypeRytmique.Croche:
                    Ellipsenote.Fill = null;
                    Ellipsenote.StrokeThickness = 1;
                    Ellipsenote.Stroke = Brushes.Black;
                    Ellipsenote.Fill = new SolidColorBrush(Colors.Black);
                    croche1.Visibility = Visibility.Visible;

                    ; break;


                case Note.TypeRytmique.DoubleCroche:
                    Ellipsenote.Fill = null;
                    Ellipsenote.StrokeThickness = 1;
                    Ellipsenote.Stroke = Brushes.Black;
                    Ellipsenote.Fill = new SolidColorBrush(Colors.Black);
                    croche1.Visibility = Visibility.Visible;
                    croche2.Visibility = Visibility.Visible;

                    ; break;


                case Note.TypeRytmique.TripleCroche:
                    Ellipsenote.Fill = null;
                    Ellipsenote.StrokeThickness = 1;
                    Ellipsenote.Stroke = Brushes.Black;
                    Ellipsenote.Fill = new SolidColorBrush(Colors.Black);
                    croche1.Visibility = Visibility.Visible;
                    croche2.Visibility = Visibility.Visible;
                    croche3.Visibility = Visibility.Visible;

                    ; break;


                case Note.TypeRytmique.QuadrupleCroche:
                    Ellipsenote.Fill = null;
                    Ellipsenote.StrokeThickness = 1;
                    Ellipsenote.Stroke = Brushes.Black;
                    Ellipsenote.Fill = new SolidColorBrush(Colors.Black);
                    croche1.Visibility = Visibility.Visible;
                    croche2.Visibility = Visibility.Visible;
                    croche3.Visibility = Visibility.Visible;
                    croche4.Visibility = Visibility.Visible;

                    ; break;

            }


            switch (note.alteration)
            {
                case Note.Alteration.No: AlterationGauche.Visibility = Visibility.Hidden; break;
                case Note.Alteration.Diese: AlterationGauche.Visibility = Visibility.Visible; AlterationGauche.Content = "#"; break;
                case Note.Alteration.Bemol: AlterationGauche.Visibility = Visibility.Visible; AlterationGauche.Content = "b"; break;
                case Note.Alteration.Becard: ; break;
            }

            /* On met à jour la largeur prise par la note selon le rithme  */
            int tailleMin = 20;
            switch (note.rythme)
            {

                case Note.TypeRytmique.QuadrupleCroche: tailleMin = 20; break;
                case Note.TypeRytmique.TripleCroche: tailleMin += 4; break;
                case Note.TypeRytmique.DoubleCroche: tailleMin += 4 * 2; break;
                case Note.TypeRytmique.Croche: tailleMin += 4 * 3; break;
                case Note.TypeRytmique.Noire: tailleMin += 4 * 4; break;
                case Note.TypeRytmique.Blanche: tailleMin += 4 * 10; break;
                case Note.TypeRytmique.Ronde: tailleMin += 4 * 20; break;
            }

            Width = tailleMin;
            FondNote.Width = tailleMin;
            //Si la note est pointées
            if (note.pointure == Note.Pointure.Pointe)
            {
                Width = Width + Width / 2;
                FondNote.Width = Width;
            }
            if (note.pointure == Note.Pointure.DoublePointe)
            {
                Width = Width + Width;
                FondNote.Width = Width;
            }

            // Infobulle
            String[] noteName = note.ToString(App.option.LangueNotationMusicale);
            Tool.Content = "\nNote : " + noteName[0] + noteName[1] + "\nOctave : " + note.octave + "\nFreq : " + note.freqHz + "\nRythme : " + noteName[3];
        }


        /* Quand le rythme change, on change la place que prends la note */
        private void RythmChanged(object sender, EventArgs e)
        {
                   Update();
          
        }
        // sens de la tige
        public void SensTige(bool TigeBas)
        {
            if (TigeBas)
            {
                tigeBas.Visibility = Visibility.Visible;
                Tige.Visibility = Visibility.Hidden;
                note.affichage.sens = Note.Affichage.SensTige.Bas;
                croche1.RenderTransform = (TransformGroup)FindResource("transformCroche2");
                Canvas.SetLeft(croche1,6);
                Canvas.SetTop(croche1,49);
                croche2.RenderTransform = (TransformGroup)FindResource("transformCroche2");
                Canvas.SetLeft(croche2,6);
                Canvas.SetTop(croche2,46);
                croche3.RenderTransform = (TransformGroup)FindResource("transformCroche2");
                Canvas.SetLeft(croche3,6);
                Canvas.SetTop(croche3,43);
                croche4.RenderTransform = (TransformGroup)FindResource("transformCroche2");
                Canvas.SetLeft(croche4,6);
                Canvas.SetTop(croche4,40);
                

            }
            else
            {
                tigeBas.Visibility = Visibility.Hidden;
                Tige.Visibility = Visibility.Visible;
                note.affichage.sens = Note.Affichage.SensTige.Haut;
                croche1.RenderTransform = (TransformGroup)FindResource("transformCroche3");
                Canvas.SetLeft(croche1, 15);
                Canvas.SetTop(croche1, 3);
                croche2.RenderTransform = (TransformGroup)FindResource("transformCroche3");
                Canvas.SetLeft(croche2, 15);
                Canvas.SetTop(croche2, 7);
                croche3.RenderTransform = (TransformGroup)FindResource("transformCroche3");
                Canvas.SetLeft(croche3, 15);
                Canvas.SetTop(croche3, 11);
                croche4.RenderTransform = (TransformGroup)FindResource("transformCroche3");
                Canvas.SetLeft(croche4, 15);
                Canvas.SetTop(croche4, 15);
            }
            Update();
        }

        /******************** Menu Contextuel ******************************/
        private void MenuItemRythm_Click(object sender, RoutedEventArgs e)
        {
            switch (((MenuItem)sender).Header.ToString())
            {
                case "Ronde": note.rythme = Note.TypeRytmique.Ronde; Update(); break;
                case "Blanche": note.rythme = Note.TypeRytmique.Blanche; Update(); break;
                case "Noire": note.rythme = Note.TypeRytmique.Noire; Update(); break;
                case "Croche": note.rythme = Note.TypeRytmique.Croche; Update(); break;
                case "Double croche": note.rythme = Note.TypeRytmique.DoubleCroche; Update(); break;
                case "Triple croche": note.rythme = Note.TypeRytmique.TripleCroche; Update(); break;
                case "Quadruple croche": note.rythme = Note.TypeRytmique.QuadrupleCroche; Update(); break;
            
            }

        }

        
    }
}
